package com.ludaxu.zhbj.base.impl;

import java.util.ArrayList;

import android.R.integer;
import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.ludaxu.zhbj.MainActivity;
import com.ludaxu.zhbj.base.BaseMenuDetailPager;
import com.ludaxu.zhbj.base.BasePager;
import com.ludaxu.zhbj.base.menudetail.InteractMenuDetailPager;
import com.ludaxu.zhbj.base.menudetail.NewsMenuDetailPager;
import com.ludaxu.zhbj.base.menudetail.PhotoMenuDetailPager;
import com.ludaxu.zhbj.base.menudetail.TopicMenuDetailPager;
import com.ludaxu.zhbj.domain.NewsData;
import com.ludaxu.zhbj.domain.NewsData.NewsMenuData;
import com.ludaxu.zhbj.fragment.LeftMenuFragment;
import com.ludaxu.zhbj.global.GlobalContants;
import com.ludaxu.zhbj.utils.CacheUtils;

/**
 * 新闻中心
 * 
 * @author ludaxu
 * 
 */
public class NewsCenterPager extends BasePager {
	// 准备4个菜单详情页的集合
	private ArrayList<BaseMenuDetailPager> mPagers;
	private NewsData mNewsData;

	public NewsCenterPager(Activity activity) {
		super(activity);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void initData() {
		tvTitle.setText("新闻");
		// 显示侧边栏
		setSlidingMenuEnable(true);

		String cache = CacheUtils.getCache(mActivity,
				GlobalContants.CATEGORIES_URL);
		if (!TextUtils.isEmpty(cache)) {// 如果缓存存在,直接解析数据,无需要访问网络
			parseData(cache);
		} else {
			getDataFromServer();
		}

	}

	/**
	 * 从服务器获取网络数据onSuccess,onFailure都是在主线程中执行
	 */
	private void getDataFromServer() {
		// Xutils的方法
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL,
				new RequestCallBack<String>() {// String表示我们从服务器拿到的是String,如果是得到文件,则参数为File
					// 访问成功
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						// Toast.makeText(mActivity,
						// "responseInfo:" + responseInfo.result, 1)
						// .show();
						// 得到json的内容
						String result = responseInfo.result;
						// 解析json,使用Gson开源框架
						parseData(result);
						// 设置缓存
						CacheUtils.setCache(mActivity,
								GlobalContants.CATEGORIES_URL, result);

					}

					// 访问失败
					@Override
					public void onFailure(HttpException error, String msg) {
						Toast.makeText(mActivity,
								"error:" + error + "msg:" + msg, 1).show();
					}
				});

	}

	/**
	 * 解析网络数据json
	 */
	protected void parseData(String result) {
		// Gson对象
		Gson gson = new Gson();
		mNewsData = gson.fromJson(result, NewsData.class);
		// System.out.println(data);
		// 得到MainActivity对象
		MainActivity mainUi = (MainActivity) mActivity;
		// 通过MainActivity对象找到fragment对象
		LeftMenuFragment leftMenuFragment = mainUi.getLeftMenuFragment();
		// 通过fragment对象,调用fragment的方法,传递数据
		leftMenuFragment.setMenuData(mNewsData);

		// 准备4个菜单详情页
		mPagers = new ArrayList<BaseMenuDetailPager>();
		mPagers.add(new NewsMenuDetailPager(mActivity,
				mNewsData.data.get(0).children));
		mPagers.add(new TopicMenuDetailPager(mActivity));
		mPagers.add(new PhotoMenuDetailPager(mActivity,btnPhoto));
		mPagers.add(new InteractMenuDetailPager(mActivity));
		// 设置菜单详情页-新闻,为默认当前页
		setCurrentMenuDetailPager(0);
	}

	/**
	 * 设置当前菜单详情页
	 * 
	 * @param position
	 */
	public void setCurrentMenuDetailPager(int position) {
		// 获取当前要显示的菜单详情页
		BaseMenuDetailPager pager = mPagers.get(position);
		flContent.removeAllViews();// 清除之前的布局
		flContent.addView(pager.mRootView);// 将布局文件
		// 设置当前页的标题
		NewsMenuData menuData = mNewsData.data.get(position);
		tvTitle.setText(menuData.title);

		pager.initData();// 初始化当前页面的数据
		if (pager instanceof PhotoMenuDetailPager) {
			btnPhoto.setVisibility(View.VISIBLE);
		} else {
			btnPhoto.setVisibility(View.INVISIBLE);
		}

	}
}
